﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Southridge.Data;
using Southridge.Business;
using System.Windows.Input;
using System.Windows.Data;
using System.ComponentModel;
using System.Windows;

namespace Southridge.View
{
    /// <summary>
    /// 
    /// </summary>
    public  class DesignTimeViewModel
    {
        protected static ListingCollection _searchResults;


        public  FilterMetadata FilterMetadata
        {
            get
            {
                return FilterMetadata.Current; 
            }
        } 

        public static ListingCollection SearchResults
        {
            get
            {
                if (_searchResults == null)
                {
                    _searchResults = new ListingCollection();

                    for (int x = 0; x < 10; x++)
                    {
                        _searchResults.Add(new Listing
                        {
                            Address = new Address { City = "Bellevue", Address1 = "One Microsoft Way", State = "WA", ZipCode = "98052" },
                            BathroomCount = 2.5,
                            BedroomCount = 4,
                            MLS = "abcd",
                            SquareFootage = 2000,
                            Description = "Description",
                            Year = 1984, ClientRating = 4, Price = 150000.0 

                        });
                    } 
                 }
                return _searchResults;
            }
        }

        DataGridColumnsView _dataGridColumnsView = new DataGridColumnsView(); 
        public DataGridColumnsView DataGridColumnsView 
        {
            get
            {
                return _dataGridColumnsView; 
            } 
        }

         
    }


  

    public class MainViewModel : BusinessEntity 
    {

        CollectionViewSource _searchSource;
        ListingCollection _searchResultsCollection; 
        
        // public SearchFilter Filter;

        ICollectionView _searchResults;

        protected SearchFilter _filter = new SearchFilter() ; 
        public SearchFilter SearchFilter
        {
            get
            {
                return _filter; 
            } 
        } 
        public ListingCollection Results
        {
            get
            {
                if (_searchResultsCollection == null)
                {
                    _searchResultsCollection  = new ListingCollection(
                    (new SouthridgeDB()).Listing.Include("Address").ToList<Listing>());
                } 
                return _searchResultsCollection; 
            }
            set
            {
                if (_searchResultsCollection != value)
                {
                    _searchResultsCollection = value;
                    OnPropertyChanged("Results");
                } 
               
            } 
        } 



       

        DataGridColumnsView _dataGridColumnsView = new DataGridColumnsView();
        public DataGridColumnsView DataGridColumnsView
        {
            get
            {
                return _dataGridColumnsView;
            }
        }

        private Profile _currentProfile; 
        public Profile CurrentProfile
        {
            get
            {
                if (_currentProfile == null)
                    _currentProfile = new Profile(); 
                return _currentProfile; 
            }
            set
            {
                if (_currentProfile != value)
                {
                    _currentProfile = value;
                    OnPropertyChanged("CurrentProfile"); 
                } 
            }
        }

        public FilterMetadata FilterMetadata
        {
            get
            {
                return FilterMetadata.Current;
            }
        }

        public MostRecentFiles MostRecentFiles
        {
            get
            {
                return MostRecentFiles.SingletonNotImplemented;     
            } 

        }

        
        public ICommand ShowColumnCommand
        {
            get
            {
                if (_showColumnCommand == null)
                {
                    _showColumnCommand = new DelegateCommand<object>(OnShowColumn, false);  
                }
                return _showColumnCommand; 
            }              
        } 

        public ICommand FilterChangedCommand
        {
            get
            {
                if ( _filterChangedCommand == null) 
                {
                    _filterChangedCommand = new DelegateCommand<object>(OnFilterChanged, false); 
 
                } 
                return _filterChangedCommand; 
            } 
        }

        public ICommand NotImplementedEnabledCommand
        {
            get
            {
                if (_notImplementedEnabledCommand == null)
                {
                    _notImplementedEnabledCommand = new DelegateCommand<object>(OnNotImplemented, false); 
                }
                return _notImplementedEnabledCommand; 
            } 
        }

        public ICommand SearchResultsCommand
        {
            get
            {
                if (_searchResultsCommand == null)
                {
                    _searchResultsCommand = new DelegateCommand<object>(OnSearchResults, false);
                }
                return _searchResultsCommand; 
            } 
        }

        protected void OnSearchResults(object o)
        { } 

        public ICommand DetailsCommand 
        {
            get
            {
                if (_detailsCommand == null)
                {
                    _detailsCommand = new DelegateCommand<object>(OnDetails, false);
                }
                return _detailsCommand; 
            }
        }

        public ICommand OpenProfile
        {
            get
            {
                if (_openProfileCommand == null)
                {
                    _openProfileCommand = new DelegateCommand<object>(OnOpenProfile, false);
                }
                return _openProfileCommand;
            }
        }

        public ICommand NewProfile
        {
            get
            {
                if (_newProfileCommand == null)
                {
                    _newProfileCommand = new DelegateCommand<object>(OnNewProfile, false);
                }
                return _newProfileCommand;
            }
        }

        public ICommand SaveProfile
        {
            get
            {
                if (_saveProfileCommand == null)
                {
                    _saveProfileCommand = new DelegateCommand<object>(OnSaveProfile, false);
                }
                return _saveProfileCommand;
            }
        }

        protected void OnOpenProfile(object o)
        {
            string name = o as string;
            if (name == null || name == string.Empty)
                return;

            this.CurrentProfile = new Profile();
            if (name == "Jaime Rodriguez")
            {
                this.CurrentProfile.FirstName = "Jaime";
                this.CurrentProfile.LastName = "Rodriguez";
            }
            else if (name == "Mark Wilson Thomas")
            {
                this.CurrentProfile.FirstName = "Mark";
                this.CurrentProfile.LastName = "Wilson Thomas";
            }

            
            this.CurrentProfile.HomePhoneNo = "123-1234";
            this.CurrentProfile.MobilePhoneNo = "123-1234";

            
            this.CurrentProfile.Address.Address1 = "One Microsoft Way";
            this.CurrentProfile.Address.Address2 = "Bldg 20";
            this.CurrentProfile.Address.State = "WA";
            this.CurrentProfile.Address.City = "Redmond";
            this.CurrentProfile.Address.ZipCode = "98052";

             
            this.CurrentProfile.Preference.Acreage = 8000;
            this.CurrentProfile.Preference.PriceStart = 200000;
            this.CurrentProfile.Preference.PriceStop = 300000;
            this.CurrentProfile.Preference.MinBaths = 2;
            this.CurrentProfile.Preference.HasView = true;
            this.CurrentProfile.Preference.HasWaterFront = true;
            this.CurrentProfile.Preference.MinGarage = 4;
            this.CurrentProfile.Preference.MinRooms = 5;
            this.CurrentProfile.Preference.SquareFeet = 2000;
            this.CurrentProfile.Preference.YearsOld = 1950;



        }

        protected void OnNewProfile(object o)
        {

            this.CurrentProfile = new Profile(); 

            FrameworkElement fe = ViewManager.Current.GetView(Views.Profile);
             
            if (fe != null)
            {
                MainViewModel mv = this;    
                fe.DataContext = this;
                ViewManager.Current.Transition(fe);
            } 
        }

        protected void OnSaveProfile(object o)
        {

        } 

        protected void OnDetails(object o)
        { } 

        public ICommand MapCommand
        {
            get
            {
                if (_mapCommand == null)
                {
                    _mapCommand = new DelegateCommand<object>( OnMapExecuted, false);
                }
                return _mapCommand;                  
            }
        }

        public ICommand ShowDetails 
        {
            get
            {
                if ( _showDetailsCommand == null)
                {
                    _showDetailsCommand = new DelegateCommand<object>(OnShowDetails, false);
                }
                return _showDetailsCommand;
            }
        }

        protected void OnShowDetails ( object param ) 
        {

            DetailsWindow window = new DetailsWindow();
            ViewManager.Current.ShowWindow(window, true); 
        } 

        protected void OnMapExecuted(object o)
        { 
        
        } 


        protected DelegateCommand<object> _filterChangedCommand ;
        protected DelegateCommand<object> _showColumnCommand ;
        protected DelegateCommand<object> _notImplementedEnabledCommand;
        protected DelegateCommand<object> _searchResultsCommand;
        protected DelegateCommand<object> _mapCommand;
        protected DelegateCommand<object> _detailsCommand;
        protected DelegateCommand<object> _openProfileCommand;
        protected DelegateCommand<object> _newProfileCommand;
        protected DelegateCommand<object> _saveProfileCommand;
        protected DelegateCommand<object> _showDetailsCommand;


        
        protected bool doOnce = false;

        protected void OnShowColumn (object param)
        {

        }

        protected void OnNotImplemented(object param)
        {
            // 
        } 
        protected void OnFilterChanged(object param)
        {

            if (param != null && param is string)
            {
                string command = param as string;
                string[] commandparams = command.Split(',');

                double d = 0;

                if (commandparams.Length != 2)
                    return;

                if (!double.TryParse(commandparams[1], out d))
                    return;

                if (commandparams[0] == "Bathroom")
                {
                    SearchFilter.MinBathrooms = d;
                }
                else if (commandparams[0] == "Bedroom")
                {
                    SearchFilter.MinBedrooms = d;
                }
            }
            else
            {  
                return;
            } 
            ICollectionView view = CollectionViewSource.GetDefaultView(_searchResultsCollection);
            if (view != null && view.Filter == null )
            {
                view.Filter = new Predicate<object>(OnCheckItem);
            }
            else if ( view != null ) 
                view.Refresh(); 
        }

        protected bool OnCheckItem(object o)
        {
            Listing li = o as Listing;
            bool match = true;
            if (li != null)
            {
                if (SearchFilter.MinBathrooms.HasValue)
                {
                    match &= (li.BathroomCount >= SearchFilter.MinBathrooms.Value);
                }
                if (SearchFilter.MinBedrooms.HasValue)
                    match &= (li.BedroomCount >= SearchFilter.MinBedrooms.Value);

            } 
            return match; 
        } 

        protected void OnApplyFilter(object sender, FilterEventArgs e)
        {
            Listing listing = e.Item as Listing;

            if (listing != null)
            {
                if (listing.BedroomCount == 3)
                    e.Accepted = true;

                else
                    e.Accepted = false;
            }
            else
                e.Accepted = false; 

        } 
    }
}
